package com.zian.controller.user;


import com.zian.common.R;
import com.zian.domain.dto.*;
import com.zian.domain.po.SysCarousel;
import com.zian.domain.po.SysUser;
import com.zian.domain.query.UserQuery;
import com.zian.domain.vo.UserQueryVO;
import com.zian.service.ISysUserService;
import com.zian.validation.UserCroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Slf4j
@Api(tags = "用户接口")
@RestController("SysUserControllerUser")
@RequestMapping("/user/users")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService userService;


    @GetMapping("/page")
   @ApiOperation("分页查询")
    public R<PageDTO<UserQueryVO>> queryPage(UserQuery query) {
        log.info("分页查询参数：{}", query);
        return R.ok(userService.queryPage(query));
    }

    @PutMapping
   @ApiOperation("禁用或者启用")
    public R updateLockedByIds(@RequestBody UserLockedDTO dto) {
        log.info("禁用或者启用参数：{}", dto);
        userService.updateLockedByIds(dto);
        return R.ok();
    }
   @PostMapping("/login")
   @ApiOperation("用户登录")
    public R<String> login(@RequestBody @Validated(UserCroup.Crud.UserLogin.class) UserLoginDTO dto) {
        log.info("用户登录参数：{}", dto);
        String jwt = userService.login(dto);
        return R.ok(jwt);
    }

    // 注册
    @PostMapping("/register")
   @ApiOperation("用户注册")
    public R register(@RequestBody @Validated UserLoginDTO dto) {
        log.info("用户注册参数：{}", dto);
        userService.register(dto);
        return R.ok();
    }

    @GetMapping("/info")
   @ApiOperation("查询用户信息")
    public R<SysUser> getInfo() {
        log.info("查询用户信息");
        SysUser user = userService.getInfo();
        return R.ok(user);
    }

    // 修改信息
    @PutMapping("/info")
   @ApiOperation("修改用户信息")
    public R updateInfo(@RequestBody @Validated UpdateUserInfoDTO dto) {
        log.info("修改用户信息：{}", dto);
        userService.updateInfo(dto);
        return R.ok();
    }

    // 修改密码
    @PutMapping("/update-pwd")
   @ApiOperation("修改密码")
    public R updatePassword(@RequestBody @Validated UpdatePwdDTO dto) {
        log.info("修改密码：{}", dto);
        userService.updatePassword(dto);
        return R.ok();
    }


    // 退出登录
    @PostMapping("/logout")
   @ApiOperation("退出登录")
    public R logout(HttpServletRequest request) {
        log.info("退出登录");
        userService.logout(request);
        return R.ok();
    }


}
